<?php 
global $_GPC, $_W;
$shopid=check_right(3,0);
$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and id='".$shopid."'");
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
load()->model('mc');

if($operation=="display"){
    if (!$_W['isfounder'] && $_W['role']!="manager"){
        $_uid=$_GPC['__uid'];
        if($_uid==NULL)message('请登陆', "", 'error');
        $user=pdo_fetch("SELECT * FROM ".tablename('users')." WHERE uid=:a ",array(":a"=>$_uid));
        $muser=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_user')." WHERE weid = '{$_W['uniacid']}' and docking=:a and status=1 ",array(":a"=>$_uid));
        if(!$muser)message('请绑定管理身份', "", 'error');
        $shopid=$muser["pcate"];
    }
	$where=$shopid ? " and id='".$shopid."'":"";
	$list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid='{$_W['uniacid']}' $where order by id desc ");
	$list2=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid='{$_W['uniacid']}' order by id desc ");
	$user=pdo_fetchall("SELECT pcate FROM ".tablename('j_hmoney_user')." WHERE weid = '{$_W['uniacid']}' order by id desc ");
	$userList=array();
	foreach($user as $row){
		if(!isset($userList[$row['pcate']]))$userList[$row['pcate']]=0;
		$userList[$row['pcate']]=intval($userList[$row['pcate']])+1;
	}
} elseif ($operation == 'set') {
	$id=$_GPC['id'];
    if (!$_W['isfounder'] && $_W['role']!="manager"){
        $_uid=$_GPC['__uid'];
        if($_uid==NULL)message('请登陆', "", 'error');
        $user=pdo_fetch("SELECT * FROM ".tablename('users')." WHERE uid=:a ",array(":a"=>$_uid));
        $muser=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_user')." WHERE weid = '{$_W['uniacid']}' and docking=:a and status=1",array(":a"=>$_uid));
        if(!$muser)message('请绑定管理身份', "", 'error');
    }
    if($muser["groupid"])$id=$muser["groupid"];
	isetcookie('shopid', $id);
	message("设置成功", $this->createWebUrl('shop'), 'success');
	
} elseif ($operation == 'post') {
	$id=$_GPC['id'];
	if($id){
		$category=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE id = :id ",array(':id'=>$id));
	}
	
	if (checksubmit('submit')){
		$memberlevel=array();
		foreach($_GPC['memberlevel'] as $index=>$val){
			$memberlevel[$index]=intval($val) ? intval($val) : 100;
		}
		$chargelist=array();
		
		foreach($_GPC['charge-key'] as $index=>$val){
			$chargelist[$val]=$_GPC['charge-val'][$index];
		}
		ksort($chargelist);
		
		$btnlist=array();
		foreach($_GPC['link-key'] as $index=>$val){
			$btnlist[$val]=$_GPC['link-val'][$index];
		}
		
		$data=array(
			'weid'=>$_W['uniacid'],
			'logo' => trim($_GPC['logo']),
			'bg' => trim($_GPC['bg']),
			'companyname'=>$_GPC['companyname'],
			'description'=>$_GPC['description'],
			'appid' => trim($_GPC['appid']),
			'app_id' => trim($_GPC['app_id']),
			'sub_appid' => trim($_GPC['sub_appid']),
			'sub_mch_id' => trim($_GPC['sub_mch_id']),
			'appsecret' => trim($_GPC['appsecret']),
			'pay_name' => $_GPC['pay_name'],
			'pay_mchid' => $_GPC['pay_mchid'],
			'pay_signkey' => $_GPC['pay_signkey'],
			'app_id' => trim($_GPC['app_id']),
			'pay_signkey' => $_GPC['pay_signkey'],
			'needtable' => intval($_GPC['needtable']),
			'wechat_status' => intval($_GPC['wechat_status']),
			'ali_status' => intval($_GPC['ali_status']),
			'cash_status' => intval($_GPC['cash_status']),
			'member_status' => intval($_GPC['member_status']),
			'refunder'=>trim($_GPC['refunder']),
			'tempid'=>trim($_GPC['tempid']),
			'tempid2'=>trim($_GPC['tempid2']),
			'tempurl'=>trim($_GPC['tempurl']),
			'qrcode'=>trim($_GPC['qrcode']),
			'creadit'=>trim($_GPC['creadit']),
			'creditbtn'=>trim($_GPC['creditbtn']),
			'printpagewidth'=>trim($_GPC['printpagewidth']),
			'printnum'=>trim($_GPC['printnum']),
			'payreceipt'=>intval($_GPC['payreceipt']),
			'printtype'=>intval($_GPC['printtype']),
			'printparam'=>json_encode($printparam),
			'memberid'=>trim($_GPC['memberid']),
			'style'=>trim($_GPC['style']),
			'docking'=>intval($_GPC['docking']),
			'docking_userid'=>intval($_GPC['docking_userid']),
			'alipay_key'=>trim($_GPC['alipay_key']),
			"alipay_store_id"=>$_GPC['alipay_store_id'] ,
			'alipay_cert'=>trim($_GPC['alipay_cert']),
			'appauthtoken'=>trim($_GPC['appauthtoken']),
			'taste'=>str_replace("，",",",trim($_GPC['taste'])),
			'memberlevel'=>json_encode($memberlevel),
			'chargelist'=>json_encode($chargelist),
			'btnlist'=>json_encode($btnlist),
			'mindiscount'=>intval($_GPC['mindiscount']),
			'servicetype'=>trim($_GPC['servicetype']),
			'customgood'=>trim($_GPC['customgood']),
			'refundreason'=>trim($_GPC['refundreason']),
			
			'custom_pay1'=>trim($_GPC['custom_pay1']),
			'custom_pay2'=>trim($_GPC['custom_pay2']),
			'custom_pay3'=>trim($_GPC['custom_pay3']),
			'custom_pay4'=>trim($_GPC['custom_pay4']),
			'custom_pay1_status'=>intval($_GPC['custom_pay1_status']),
			'custom_pay2_status'=>intval($_GPC['custom_pay2_status']),
			'custom_pay3_status'=>intval($_GPC['custom_pay3_status']),
			'custom_pay4_status'=>intval($_GPC['custom_pay4_status']),

            'bankmch_id'=>trim($_GPC['bankmch_id']),
            'bankpaykey'=>trim($_GPC['bankpaykey']),

            'customerid'=>trim($_GPC['customerid']),
            'customerkey'=>trim($_GPC['customerkey']),
		);
		if(!$id){
            if (!$_W['isfounder'] && $_W['role']!="manager")die("权限不足");
			$isUsed=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and companyname=:a",array(":a"=>$data['companyname']));
			if($isUsed)message("【".$data['companyname']."】已经被使用");
            /**/
            $allnum=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}'");
            if($cfg["limitnum"] && $allnum>$cfg["limitnum"]){
                message("超出可添加店铺数量，可添加【".$allnum."】家","","error");
            }
			pdo_insert("j_hmoney_group",$data);
			$id=pdo_insertid();
		}
		load()->func('file');
		$dir_url='../attachment/j_hmoney/cert_2/'.$_W['uniacid']."/".$id."/";
		mkdirs($dir_url);
		if ($_FILES["apiclient_cert"]["name"]){
			if(file_exists($dir_url."apiclient_cert.pem"))@unlink($dir_url."apiclient_cert.pem");
			$data['apiclient_cert']="apiclient_cert.pem";
			move_uploaded_file($_FILES["apiclient_cert"]["tmp_name"],$dir_url."apiclient_cert.pem");
		}
		if ($_FILES["apiclient_key"]["name"]){
			if(file_exists($dir_url."apiclient_key.pem"))@unlink($dir_url."apiclient_key.pem");
			$data['apiclient_key']="apiclient_key.pem";
			move_uploaded_file($_FILES["apiclient_key"]["tmp_name"],$dir_url."apiclient_key.pem");
		}
		pdo_update("j_hmoney_group",$data,array("id"=>$id));
		message("修改完成", $this->createWebUrl('shop',array('op'=>'post','id'=>$id)), 'success');
	}
	
}elseif ($operation == 'delete') {
    $id=intval($_GPC['id']);
    if($id){
        if (!$_W['isfounder'] && $_W['role']!="manager")message("权限不足");
        pdo_delete('j_hmoney_group',array('id'=>$id));
    }
	message("删除成功", $this->createWebUrl('shop'), 'success');
}
include $this->template('web/'.$cfg['floder'].'/shop');